package com.ruoyi.system.service.impl;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.system.service.SendSmsService;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.HashMap;
import java.util.Map;

@Service
public class SendSmsServiceImpl implements SendSmsService {


    @Override
    public boolean send(String phoneNum, String ppPassword) {

        /**
         * regionID @cn-hangzhou
         * 阿里云短信服务账号 @accessKeyId
         * 阿里云短信服务密码 @secret
         * */
        // 链接阿里云  这个不需要动 cn-hangzhou  accessKeyId : 账号  secret: 密码
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", RuoYiConfig.getAliyunkeyid(), RuoYiConfig.getAliyunkeysecret());
        IAcsClient client = new DefaultAcsClient(profile);
        // 构建请求
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");//官方说不要动
        request.setVersion("2017-05-25");//官方说不要动
        request.setAction("SendSms");//事件名称

        // 自定义的参数 （手机号,验证码,签名,模板!)
        request.putQueryParameter("PhoneNumbers",phoneNum);//手机号
        request.putQueryParameter("SignName","人社部专家中心");//签名
        request.putQueryParameter("TemplateCode","SMS_269045510");//模板CODE
        request.putQueryParameter("TemplateParam","{'password':'"+ppPassword+"'}");    // 新的密码


        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();//成功
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 自定义 循环调用
    @Override
    public boolean sendZDY(String phoneNum, String mubanid,Map<String,Object> ppMap) {

        /**
         * regionID @cn-hangzhou
         * 阿里云短信服务账号 @accessKeyId
         * 阿里云短信服务密码 @secret
         * */
        // 链接阿里云  这个不需要动 cn-hangzhou  accessKeyId : 账号  secret: 密码
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", RuoYiConfig.getAliyunkeyid(), RuoYiConfig.getAliyunkeysecret());
        IAcsClient client = new DefaultAcsClient(profile);
        // 构建请求
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");//官方说不要动
        request.setVersion("2017-05-25");//官方说不要动
        request.setAction("SendSms");//事件名称

        // 自定义的参数 （手机号,验证码,签名,模板!)
        request.putQueryParameter("PhoneNumbers",phoneNum);//手机号
        request.putQueryParameter("SignName","人社部专家中心");//签名
        request.putQueryParameter("TemplateCode",mubanid);//模板CODE
        request.putQueryParameter("TemplateParam",JSONObject.toJSONString(ppMap));    // 替换标签


        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();//成功
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

}

